<?php
/**
 * Created by PhpStorm.
 * User: 86188
 * Date: 2021/1/18
 * Time: 10:13
 */

namespace app\admin\controller;

use app\service\apiinvoiceService;
use app\service\billService;
use app\service\invoiceService;
use think\Controller;
use think\Db;

class Yzlonline extends Controller
{
    public function test(){

        $id=7739;

        $info=$this->invoiceData($id);

        $api=new apiinvoiceService();
        $res=$api->invoiceSendDirect(json_encode($info));



        echo "<pre>";
        var_dump($info);
        var_dump($res);die;

    }
    public function invoiceData($id){

        $billpayorder=Db::table("billpayonline")->where("id",$id)->find();
        $billinvoice=Db::table("billinvoice")->where("pay_id",$id)->where("ctype","2")->find();
        $communityinfo=Db::table("community")->where("community_id",$billinvoice["community_id"])->field("property_id,community_name")->find();

        //根据物业查询发票信息

        $serviceinfo=(new invoiceService())->propertyInvoice($communityinfo["property_id"]);
        //return $serviceinfo;
        //var_dump($info);
        $data["m_short_name"]=$serviceinfo["m_short_name"];
        $data["sub_m_short_name"]=$serviceinfo["sub_m_short_name"];
        $data["user_id"]=$billinvoice["userid"];


        $data["invoice_date"]=$billinvoice["invoice_date"];


        $billid=unserialize($billinvoice["billid"]);
        $item=array();
        $tax_money=0;
        foreach ($billid as $key =>$value){
            $billinfo = (new billService())->billIdinfo($value);


            $taxrate = (new billService())->taxRateCommunity($billinfo['cost_type'],$billinfo['community_id']);
            if(empty($taxrate)){
                $taxrate ="0.06";
            }
            $tax_amount=round($taxrate*$billinfo["bill_entry_amount"],2);
            $tax_money=round($tax_money+$tax_amount,2);

            $item[]="{" .
                " \"item_name\":\"".$billinfo["cost_type"]."\"," .
                "\"item_no\":\"".$billinfo["bill_entry_id"]."\"," .
                "\"item_spec\":\"1\"," .
                "\"item_unit\":\"条\"," .
                "\"item_quantity\":\"1\"," .
                "\"item_unit_price\":\"".$billinfo["bill_entry_amount"]."\"," .
                "\"item_ex_tax_amount\":\"".$billinfo["bill_entry_amount"]."\"," .
                "\"item_tax_rate\":\"".$taxrate."\"," .
                "\"item_tax_amount\":\"".$tax_amount."\"," .
                "\"item_sum_amount\":\"".$billinfo["bill_entry_amount"]."\"," .
                "\"row_type\":\"0\"" .
                "}";

        }

        $data["item"] = implode(',', $item);
        $data["sum_amount"]=$billinvoice["pay_money"];
        $data["tax_amount"]=$tax_money;//合计税额，票面上的合计税额
        $data["ex_tax_amount"]=$data["sum_amount"]-$data["tax_amount"];//不含税金额
        $data["out_trade_no"]=$billpayorder["trade_no"];
        $data["invoice_type"]='BLUE';
        $data["invoice_kind"]='PLAIN';

        $data["invoice_code"]=$billinvoice["invoiceCode"];//发票代码，该字段与invoice_no构成一张发票的唯一标识
        $data["invoice_no"]=$billinvoice["invoiceNum"];//发票号码，该字段与invoice_code构成一张发票的唯一标识。
        //购买方信息
        //$roominfo=Db::table("billinvoice")->where("out_room_id",$billinvoice["out_room_id"])->find();
        $roominfo=Db::table("roominfo")->where("out_room_id",$billinvoice["out_room_id"])->find();
        $data["title_name"]=$communityinfo["community_name"]."-".$roominfo["group"]."-".$roominfo["building"]."-".$roominfo["unit"]."-".$roominfo["room"];

        $data["payer_register_no"]="";//购方税号
        $data["payer_address_tel"]="";
        $data["payer_bank_name_account"]="";

        //销售方信息
        $data["payee_register_no"]=$serviceinfo["fp_saler_tax_num"];
        $data["payee_register_name"]=$serviceinfo["name"];
        $data["payee_address_tel"]=$serviceinfo["fp_saler_account"];

        $data["payee_bank_name_account"]=$serviceinfo["bank"];
        $data["check_code"]='';
        //$data["out_invoice_id"]=$info["orderNo"];
        $data["out_invoice_id"]=date("YmdHis").rand(100000,999999);
        $data["ori_blue_inv_code"]='';
        $data["ori_blue_inv_no"]='';
        $data["file_download_type"]='PDF';
        $data["file_download_url"]=$billinvoice["invoiceFileUrl"];
        $data["payee"]='';
        $data["checker"]='';
        $data["clerk"]='';
        $data["invoice_memo"]="物业缴费";
        $data["xtend_fields"]='';
        return $data;

    }
}